package com.liseen.loader
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;

	public class ImageLoadItem extends LoadItem
	{
		public var loader:Loader;
		/**
		 *图片加载器
		 * @param url
		 * @param type
		 * 
		 */		
		public function ImageLoadItem(url:String,type:String)
		{
			super(url,type);
		}
		
		/**
		 *获取类定义 
		 * @param name
		 * @return 
		 * 
		 */		
		public function getDefinitionByName(name:String):Class
		{
			if(loader.contentLoaderInfo.applicationDomain.hasDefinition(name))
			{
				return loader.contentLoaderInfo.applicationDomain.getDefinition(name) as Class;
			}
			return null;
		}
		/**
		 *停止加载 
		 * 
		 */		
		override public function stop():void
		{
			try{
				if(this.loader)
				{
					this.loader.close();
				}
			}catch(e:Error){
				
			}
			super.stop();
		}
		
		/**
		 *加载完成 
		 * @param event
		 * 
		 */		
		override public function onCompleteHandler(event:Event):void
		{
			try{
				this.content = loader.contentLoaderInfo;
			}catch(error:SecurityError){
				this.content=null;
			}
			super.onCompleteHandler(event);
		}
		
		override public function load():void
		{
			super.load();
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.INIT, onInitHandler, false, 0, true);
			loader.contentLoaderInfo.addEventListener(Event.OPEN, onStartedHandler, false, 0, true);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onErrorHandler, false, 0, true);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.NETWORK_ERROR, onErrorHandler, false, 0, true);
			loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityErrorHandler, false, 0, true);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler, false, 0, true);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler, false, 0, true);
			loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, super.onHttpStatusHandler);
			try {
				loader.load(urlRequest,loadContext);
			} catch(e:SecurityError) {
				super.onSecurityErrorHandler(e);
			}
		}
		
		override protected function cleanListeners():void
		{
			var loaderInfo:LoaderInfo;
			if(loader)
			{
				loaderInfo = loader.contentLoaderInfo;
				loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgressHandler, false);
				loaderInfo.removeEventListener(Event.COMPLETE, onCompleteHandler, false);
				loaderInfo.removeEventListener(Event.INIT, onInitHandler, false);
				loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onErrorHandler, false);
				loaderInfo.removeEventListener(IOErrorEvent.NETWORK_ERROR, onErrorHandler, false);
				loaderInfo.removeEventListener(Event.OPEN, onStartedHandler, false);
				loaderInfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS, super.onHttpStatusHandler, false);
			}
		}
		
		/**
		 *销毁
		 * 
		 */		
		override public function destroy():void
		{
			stop();
			cleanListeners();
			this.content = null;
			this.loader = null;
		}
		
	}
}